import storage from "@/util/storage";
import api from "@/api";

const state = () => ({
    user_info: JSON.parse(storage.local.get("user_info")) || "",
    u_token: storage.local.get("u_token") || "",
    a_token: storage.local.get("a_token") || "",
    u_expire_time: storage.local.get("u_expire_time") || "",
    a_expire_time: storage.local.get("a_expire_time") || "",
    permissions: [],
    auth_unit_name: storage.local.get("auth_unit_name") || "",
    is_clear_timer_alarm: false, //监控大屏轮训标识
});

const getters = {
    isAuth: (state) => {
        let retn = false;
        if (state.a_token) {
            let unix = Date.parse(new Date());
            if (unix < state.a_expire_time * 1000) {
                retn = true;
            }
        }
        return retn;
    },
    isLogin: (state) => {
        let retn = false;
        if (state.u_token) {
            let unix = Date.parse(new Date());
            if (unix < state.u_expire_time * 1000) {
                retn = true;
            }
        }
        return retn;
    },
};

const actions = {
    // 地址栏有a-token,u-token, 获取用户信息, 存储信息
    getUserInfo({ commit }, data) {
        api({
            url: "/unit/XfUser/userAuthByToken",
            method: "get",
            headers: {
                "A-Token": data.a,
                "U-Token": data.u,
            },
        }).then((res) => {
            commit("setUserInfo", res.data);
        });
        return;
    },
    auth({ commit }, data) {
        return new Promise((resolve, reject) => {
            // 通过 mock 进行登录

            api.post("/unit/Login/auth", data)
                .then((res) => {
                    commit("setUserAuth", res.data);
                    resolve();
                })
                .catch((error) => {
                    reject(error);
                });
        });
    },
    login({ commit }, data) {
        return new Promise((resolve, reject) => {
            api.post("/unit/Login/user", data)
                .then((res) => {
                    commit("setUserData", res.data);
                    // const alarmAudio = document.getElementById('alarm_audio')
                    // alarmAudio.play()
                    // alarmAudio.pause()
                    resolve();
                })
                .catch((error) => {
                    reject(error);
                });
        });
    },
    logout({ commit }) {
        // const alarmAudio = document.getElementById('alarm_audio');
        // alarmAudio.pause();
        commit("setClearTimer", true);
        commit("removeUserData");
        commit("menu/invalidRoutes", null, {
            root: true,
        });
        commit("tabbar/clean", null, {
            root: true,
        });
        commit("menu/removeRoutes", null, {
            root: true,
        });
    },
    // 获取我的权限
    getPermissions({ state, commit }) {
        return new Promise((resolve) => {
            // 通过 mock 获取权限
            api.get("member/permission", {
                baseURL: "/mock/",
                params: {
                    account: state.account,
                },
            }).then((res) => {
                commit("setPermissions", res.data.permissions);
                resolve(res.data.permissions);
            });
        });
    },
};

const mutations = {
    //设置监控大屏清除定时器标识
    setClearTimer(state, data) {
        state.is_clear_timer_alarm = data;
    },
    // 有a-token与u-token 设置用户信息与授权信息
    setUserInfo(state, data) {
        storage.local.set("user_info", JSON.stringify(data.user));
        storage.local.set("auth_unit_name", data.unit_info);
        state.user_info = data.user;
        state.auth_unit_name = data.unit_info;
    },
    setUserAuth(state, data) {
        // storage.local.set('auth_unit_name',data.auth.auth_unit_name)
        storage.local.set("a_token", data.a_token);
        storage.local.set("a_expire_time", data.expire_time);
        state.a_token = data.a_token;
        state.a_expire_time = data.expire_time;
        // state.auth_unit_name = data.auth.auth_unit_name
    },
    setUserData(state, data) {
        // storage.local.set('account', data.account)
        storage.local.set("u_token", data.u_token);
        storage.local.set("u_expire_time", data.expire_time);
        storage.local.set("user_info", JSON.stringify(data.user_info));
        storage.local.set("unit_info", JSON.stringify(data.unit_info));
        state.user_info = data.user_info;
        state.u_token = data.u_token;
        state.u_expire_time = data.expire_time;
    },
    removeUserData(state) {
        storage.local.remove("a_token");
        storage.local.remove("u_token");
        storage.local.remove("a_expire_time");
        storage.local.remove("u_expire_time");
        storage.local.remove("user_info");
        storage.local.remove("auth_unit_name");
        state.a_token = "";
        state.u_token = "";
        state.a_expire_time = "";
        state.u_expire_time = "";
        state.user_info = "";
        state.auth_unit_name = "";
    },
    setPermissions(state, permissions) {
        state.permissions = permissions;
    },
    // 地址栏有a-token,u-token,保存
    setToken(state, data) {
        // 获取三个月后的时间戳
        var d = new Date();
        d.setMonth(d.getMonth() + 3);
        var yy1 = d.getFullYear();
        var mm1 = d.getMonth() + 1;
        var dd1 = d.getDate();
        if (mm1 < 10) {
            mm1 = "0" + mm1;
        }
        if (dd1 < 10) {
            dd1 = "0" + dd1;
        }
        let date = new Date().getTime(yy1 + "/" + mm1 + "/" + dd1);
        // let obj = {"id":11,"region_id":510600,"area_id":1,"username":"alert1","role_id":1,"name":"路凤英","sex":1,"phone":"18550702547","head_img":"","age":47,"job":"","is_stop":1,"update_time":1630552633}
        storage.local.set("u_token", data.u);
        storage.local.set("u_expire_time", date);
        storage.local.set("a_token", data.a);
        storage.local.set("a_expire_time", date);
        // storage.local.set('user_info', JSON.stringify(obj))
        // state.user_info = obj
        state.u_token = data.u;
        state.u_expire_time = date;
        state.a_token = data.a;
        state.a_expire_time = date;
    },
};

export default {
    namespaced: true,
    state,
    actions,
    getters,
    mutations,
};
